package com.sskj.mine.verify;

import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;

import com.hey.lib.HeySpinner;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.R;
import com.sskj.mine.R2;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/01
 */
public class VerifyFirstActivity extends BaseActivity<VerifyFirstPresenter> {

    @BindView(R2.id.name_edt)
    EditText nameEdt;
    @BindView(R2.id.id_card_edt)
    EditText idCardEdt;
    @BindView(R2.id.submit_btn)
    Button submitBtn;
    @BindView(R2.id.verify_type)
    HeySpinner verifyType;

    private int type = 1;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_verify_first;
    }

    @Override
    public VerifyFirstPresenter getPresenter() {
        return new VerifyFirstPresenter();
    }

    @Override
    public void initView() {
        setTitle(App.INSTANCE.getString(R.string.mine_verifyFirstActivity1_j));
        List<String> types = new ArrayList<>();
        types.add(App.INSTANCE.getString(R.string.mine_verifySecondActivity2_j));
        types.add(App.INSTANCE.getString(R.string.mine_verifySecondActivity3_j));
        verifyType.attachData(types);
        verifyType.setOnSelectListener(position -> {
            type = position + 1;
            if (type == 1) {
                idCardEdt.setHint(App.INSTANCE.getString(R.string.mine_verifyFirstActivity4_j));
            } else {
                idCardEdt.setHint(App.INSTANCE.getString(R.string.mine_verifyFirstActivity5_j));
            }
        });
    }


    @Override
    public void initData() {
        ClickUtil.click(submitBtn, () -> {
            if (isEmpty(nameEdt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifyFirstActivity6_j));
                return;
            }
            String reg = "^[\\u4e00-\\u9fa5]{2,4}";
            if (!getText(nameEdt).matches(reg)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifyFirstActivity7_j));
                return;
            }
            if (isEmpty(idCardEdt)) {
                if (type == 1) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifyFirstActivity4_j));
                } else {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifyFirstActivity5_j));
                }
                return;
            }
//            if (type == 1) {
//                if (!CheckUtil.isIdCard(getText(idCardEdt))) {
//                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifyFirstActivity8_j));
//                    return;
//                }
//            }
            mPresenter.verifyFirst(nameEdt.getText().toString(), idCardEdt.getText().toString(), type + "");
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, VerifyFirstActivity.class);
        context.startActivity(intent);
    }

    public void verifyFirstSuccess() {
        ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifyFirstActivity9_j));
        Intent intent = new Intent();
        intent.putExtra("name", nameEdt.getText().toString().trim());
        intent.putExtra("card", idCardEdt.getText().toString().trim());
        setResult(1, intent);
        finish();
    }

}
